<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    :close-on-click-modal="false"
    width="800px"
    top="50px"
    append-to-body
  >
  <!-- 活动充值界面 -->
  <div class="form-box">
   
      <div class="kuaijie flex ">
        <div
          class="kuaijie-item"
          @click="kuaijieClick(item, i)"
          :class="{ active: i === activeIndex }"
          v-for="(item, i) in huodong"
          :key="i"
        >
          <div class="jf font18">{{ item.smsNum }}短信</div>
          <div class="money font14">{{item.yudou }}虞豆</div>
          
          <!-- <div class="remark" v-html="item.remark"></div> -->
          <div class="xs">限时</div>
        </div>
      </div>
      <el-form
        ref="form"
        :model="form"
        :rules="rules"
        label-width="135px"
        class="demo-ruleForm"
      >
        <!-- <el-form-item label="自定义充值金额:" prop="price">
          <el-input-number
            :step="1000"
            v-model="form.price"
            placeholder="请输入充值金额"
            @input="getCountInfoByType"
         
          />
          <span v-if="countJifen" style="margin-left:10px">到账 <strong style="font-size:18px;color:#ff9717">{{countJifen}}</strong> 虞豆</span>
        </el-form-item> -->
        <!-- <el-form-item label="支付方式：" prop="num">
          <payWayLsit :showYue="true" :payWay='payWay' ref="payWayLsit" @selectPayway="selectPayway"></payWayLsit>
        </el-form-item> -->
        <!-- <el-form-item label="当前虞豆：" prop="num">
          <span style="color: red;font-size: 20px;">
            {{ shopInfo.campNumber||0 }}
          </span>
        </el-form-item>  -->
        <div style="line-height: 40px;">

          
          <span>
            当前虞豆:
            <span style="color: yellowgreen;font-size: 20px;">
              {{ shopInfo.campNumber||0 }}，
            </span>
          </span>
          <span>需消耗 <span style="color: red;font-size: 20px;">{{ form.price }}</span> 虞豆</span>
        </div>

        <div style="color: #666;">
          说明：
        </div>
        <div class="tip">
          短信用于商城用户登录注册时发送验证码等需要给用户发送短信的场景。<br/>
           每个商户每月有100条免费短信，超出后需购买。
        </div>

        <div class="center">
          <el-button
            type="primary"
            @click="submitForm('form')"
            v-loading="loading"
            >确认充值</el-button
          >
        </div>
      </el-form>
    </div>
  </el-dialog>
</template>
<script>
import moneyApi from "@/api/financeCenter/myMoney";
import payApi from "@/api/financeCenter/pay";
import { mapGetters } from "vuex";
import _ from 'lodash'
export default {
  name: "smschongzhi",
  components:{},
  data() {
  
    return {
      dialogVisible: false,
      loading: false,
      title: "短信包",
      activeIndex: "",
      activeInfo:null,
      countJifen:'',
      payWay:3,
      huodong:[
        {
          smsNum:10000,
          yudou:7000,
          id:1
        },
        {
          smsNum:50000,
          yudou:30000,
          id:2
        },
        {
          smsNum:100000,
          yudou:55000,
          id:3
        },
      ],
      desCont:`1.1.分销获取  说明：通过虞都互联系统成交的所有订单将按照订单金额百分之十赠送虞豆<br/>
举例:虞都平台当月帮您售卖10万元的账号销售额,您可获得1万虞豆.<br/>
2.供货获取  说明：在虞都互联系统销售出去的账号订单金额<br/>
举例：您在虞都互联卖自己的号交易金额10万则赠送1万虞豆 按照交易金额的百分之十赠送虞豆 <br/>
3.每月1-5日为虞豆兑换日 兑换商务联系方式19139006222同微信 <br/>
4.最终解释权归虞都所有.`,
        activeRemark:'',
      // 表单参数
      form: {
        price: "",
        smsNum:'',
        dxcz:''
      },
      orderId:'',
      rules: {
        price: [{ required: true, message: "请输入充值金额", trigger: "blur" }],
      },
      jieliu:0
    };
  },
  computed: {
    ...mapGetters(["userInfo", "shopInfo"]),
  },
  created() {
  },
  methods: {
    // 打开弹窗
    open() {
      this.activeIndex = ''
      this.form.price = ''
      this.loading = false;
      this.dialogVisible = true;
      this.activeIndex = 0;
      
      this.$nextTick(()=>{
        this.kuaijieClick(this.huodong[0],0)
      })
    },
    // 获取活动信息
    activitySettingsGetByType(){
      // moneyApi.activitySettingsGetByType({
      //   type:1
      // }).then(res=>{
      //   if(res.code==200){
      //     // 将/n换成<br/>
      //     let data = res.data
      //     this.activeRemark =  data.remark.replace(/\n/g, '<br/>')
      //     this.activeInfo = data
      //     this.huodong = JSON.parse(data.rule)
      //     this.form.price =  this.activeInfo&& this.activeInfo.mode==1? this.huodong[0].minAmount:this.huodong[0].maxAmount
      //     this.countComputed()
      //   }
      // })
     

    },
    // selectPayway(val){
    //   this.payWay = val
    // },
  //   // 根据金额计算应得虞豆
  //  getCountInfoByType(val){
  //   if(!val){
  //     this.countJifen = 0
  //     return
  //   }
  //   if(this.jieliu){
  //     clearTimeout(this.jieliu)
  //   }
  //     this.jieliu = setTimeout(()=>{
  //       this.countComputed()
  //     },1000)
  //   },
  //   countComputed(){
  //     moneyApi.getCountInfoByType({price:this.form.price}).then(res=>{
  //         this.countJifen = res.data.count
  //         this.jieliu = 0
  //       })
  //   },

    // 重置
    reset() {
      this.$refs.form.resetFields();
    },
    kuaijieClick(item, i) {
      this.activeIndex = i;
      this.form.price = item.yudou;
      this.form.smsNum = item.smsNum;
      this.form.dxcz = item.id;
      this.$refs["form"].validate()
      // this.countComputed()
    },
    // 确认充值
    async submitForm(formName) {
      
      this.$refs["" + formName].validate((valid) => {
        if (valid) {
          if(this.form.price>this.shopInfo.campNumber){
            return this.$message.error('虞豆不足，请先充值虞豆！')
          }
          this.loading = true;
          this.$confirm(`确认花费${this.form.price}虞豆，充值${this.form.smsNum}短信吗？`, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning",
          })
            .then(() => {
              this.form.tenantId = this.shopInfo.tenantId
              moneyApi.addDuanxin(this.form).then(async (res) => {
                this.loading = false;
                // return alert(res.data.content)
                // this.orderId = res.data.id
                // await this.payNow(res.data.id)
                // this.loading = false;
                if(res.code==200){
                  this.$message.success('充值成功！')
                  this.dialogVisible = false;
                  this.reset();
                  this.$store.dispatch("getShopInfo")
                }
                
              }).catch(err=>{
                this.loading = false;
              })
            })
            .catch(() => {
              this.loading = false;
            });
        } else {
          this.$alert("请输入充值金额！");
        }
      });
    },
    // // 轮询订单支付结果
    // checkOrderInterval(){
    //   moneyApi
    //     .queryJifenPayStateInterval({ id: this.orderId })
    //     .then((res) => {
    //       if (res.code == 200) {
    //         if (res.data.payState == 2) {
    //           this.$refs.payQrcode.showSuccess('虞豆充值','虞豆充值成功，快去使用吧！')
    //           this.$refs.payQrcode.close()
    //           this.$store.dispatch("getShopInfo")
              
    //         }
    //       }
    //     });
    // },
    // // 查询订单状态
    // checkOrder() {
    //   moneyApi
    //     .queryJifenPayState({
    //       id: this.orderId
    //     })
    //     .then((res) => {
    //       if (res.code == 200) {
    //         if (res.data.payState == 2) {
    //           this.$refs.payQrcode.showSuccess('虞豆充值','虞豆充值成功，快去使用吧！')
    //           this.$refs.payQrcode.close()
    //           this.$store.dispatch("getShopInfo")
    //         }else{
    //           this.$message.waring('未查询到支付成功！如有问题请联系客服')
    //         }
    //       }
    //     });
    // },
    // // 请求支付参数
    // async payNow(id){
    //   let obj = {
    //           id:id,
    //           payWay:this.payWay,
    //           payScene:3,
    //           qrPayMode:4,
    //           goodsTitle:'虞豆充值'
    //         }
    //         const res1 = await payApi.payNow(obj);
    //         //console.log(res1);
    //         if (res1.code === 200) {
    //           this.$refs.payQrcode.open(this.payWay,res1.data.formStr )
    //         }
    // },

    close() {
      this.reset();
      this.dialogVisible = false;
    },
  },
};
</script>
<style scoped lang="scss">
.form-box {
  padding: 10px 10px;
  .top-box{
    .flex{
        align-items: center;
      }
      flex-wrap: wrap;
    .item{
      flex-shrink: 0;
      width: 217px;
      height: 110px;
      margin-right: 20px;
      background-image: url("https://imgage.maimaihao.com/yuduhulian/erp/static/icon/jf-bg.png");
      background-size: 100% 100%;
      padding:5px 30px 0 10px;
      margin-bottom: 10px;
      position: relative;
      &:nth-of-type(3n){
        margin-right: 0;
      }
      .money{
        color: #fff;

      }
      
      .clock{
        width: 18px;
        height: 18px;
        background-image: url("https://imgage.maimaihao.com/yuduhulian/erp/static/icon/clock.png");
        background-size: 100% 100%;
        margin-right: 3px;
      }
      .xs{
       
        font-size: 16px;
        font-weight: 800;
        color: #333333;
        background: linear-gradient(180deg, #F2E36C 0%, #D1A80D 100%);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
        line-height: 16px;
      }
      .name{
        position: absolute;
        right: 0;
        bottom: 0;
        width: 60px;
        height: 18px;
        background: #FFFFFF;
        border-radius: 4px 0px 9px 0px;
        border: 1px solid #CBCAFB;
        text-align: center;
        color: #FF0000;
        font-size: 12px;
      }
      .item-bot{
        color: #000;
        margin-top: 20px;
        
        .jf{
          
          font-size: 24px;
          font-weight: 800;

        }
      }
    }
  }
  .des{
    margin-top: 10px;
    .tit{
      font-weight: bold;
    }
  }



  .sub-tit {
    font-size: 24px;
    margin-top: 10px;
  }
  .kuaijie {
    margin-top: 20px;
    flex-wrap: wrap;
    

  }
  .kuaijie-item {
    min-width: 200px;
    // width: 100%;
    // height: 49px;
    border-radius: 4px;
    border: 1px solid $yellow;
    text-align: center;
    color: $yellow;
    padding: 10px;
    margin-right: 30px;
    margin-bottom: 10px;
    cursor: pointer;
    position: relative;
    &.active {
      color: #fff;
      background-color: #ff9717;
    }
    .xs{
      background-color: #eb8200;
      width: 33px;
      height: 17px;
      font-size: 12px;
      position: absolute;
      top: 0;
      right: 0;
      color: #fff;
    }
  }
  .demo-ruleForm {
    margin-top: 20px;
  }
  .zfb {
    width: 42px;
    height: 42px;
  }
  .tip {
    font-size: 12px;
    color: rgba(0, 0, 0, 0.5);
  }
  .center {
    text-align: center;
    margin-top: 20px;
    .el-button {
      width: 293px;
      height: 48px;
      font-size: 18px;
    }
  }
}

</style>
